package com.iot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iot.domain.User;
import com.iot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wlr
 * @since 2022-01-05
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/login")
    public ModelAndView login(User user, ModelAndView modelAndView, HttpSession session){
//        userService.select
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("username",user.getUsername()).eq("password",user.getPassword());
        User user1 = userService.selectOne(queryWrapper);
        if (user1!=null){
            modelAndView.setViewName("index");
        }else {
            modelAndView.getModel().put("errorInfo","用户名或者密码错误");
//            modelAndView.getModel().put("href","<a href=\"/\">点击返回</a>\n");
            modelAndView.setViewName("error");
        }
        return modelAndView;
    }
    @RequestMapping("/register")
    public ModelAndView register(User user,ModelAndView modelAndView){
        userService.insert(user);
        modelAndView.setViewName("login");
        return modelAndView;
    }

}

